package com.iflysse.servlet.content;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.iflysse.pojo.ContentInfo;
import com.iflysse.service.ContentInfoService;
import com.iflysse.utils.PagerBean;

/**
 * Servlet implementation class ContentOptHandleServlet
 */
@WebServlet("/content/ContentOptHandleServlet.do")
public class ContentOptHandleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ContentOptHandleServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		if ("addContent".equals(method)) {
			addContent(request, response);
		} else if("editContent".equals(method))
		{
			ContentInfoService service=new ContentInfoService();
			ContentInfo info=service.findByContentId(Integer.parseInt(request.getParameter("id")));
			request.setAttribute("contentInfo",info);
			addContent(request, response);
		}
		else if("delContent".equals(method))
		{
			String id=request.getParameter("id");
			ContentInfoService service=new ContentInfoService();
			service.delByContentId(Integer.parseInt(id));
//			System.out.println(id);
			response.sendRedirect(request.getContextPath()+"/content/ContentOptRequestServlet.do");
		}
		else{
			loadContent(request, response);
		}

	}

	private void addContent(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/views/content/addContent.jsp")
				.forward(request, response);

	}

	private void loadContent(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		ContentInfoService service = new ContentInfoService();
		int page = 1;// 第一页
		int pageSize = 10;// 一页显示十条
		try {
			page = Integer.parseInt(request.getParameter("page"));
			pageSize = Integer.parseInt(request.getParameter("rows"));
		} catch (Exception e) {
		}

		PagerBean<ContentInfo> pager = new PagerBean<ContentInfo>();
		pager.setPage(page);// 当前页
		pager.setPageSize(pageSize);// 每页记录条数
		pager = service.findContentPager(pager);

		pager.setTotal(service.findContentTotal());
//		System.out.println(pager);

		PrintWriter writer = response.getWriter();
		writer.print(pager.toString());
	}

}
